﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace ArchSoft
{
    public partial class DesignViewportFrm : Form
    {
        Thread myth = null;
        string mxPth = "";
        ProgressingWin fr = null;
        public DesignViewportFrm(string MxSupPath)
        {
            mxPth = MxSupPath;
            InitializeComponent();            
        }

        public DesignViewportFrm()
        {
            InitializeComponent();            
        }


        private void button2_Click(object sender, EventArgs e)
        {               
            myth = new Thread(ReloadAllDrawingMain);
            myth.IsBackground = true;
            fr = new ProgressingWin(myth);
            //fr.MessText = "Design Viewport successfully.";
            //fr.Text = "Design Viewport";
            fr.MessText = "Thiết kế khung nhìn thành công.";
            fr.Text = "Thiết kế khung nhìn";
            this.Opacity = 0;
            //this.ShowInTaskbar = false;
            fr.ShowDialog();
            Close();
        }

        public void ReloadAllDrawingMain()
        {
            string Szfl = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Trang\\" + ProjectManager.DEFAULT_APP_NAME + "\\Viewport";
            foreach (string fl in Directory.GetFiles(Szfl))
            {
                Szfl = fl;
                if (fl.IndexOf(this.textBox1.Text.Substring(0, 2) + ".dwg") >= 0)
                {
                    Szfl = fl;
                    break;
                }
            }
            InsertSize2MainViewportFile binder = new InsertSize2MainViewportFile(mxPth,Szfl);
            binder.Process();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void DesignViewportFrm_Load(object sender, EventArgs e)
        {
            //Load General!!
            try
            {
                //Load Noi dung File Dinh nghia(neu ton tai)!!!
                string cpath = Directory.GetParent( this.mxPth).ToString() + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
                DataTable tbConsl = null;
                if (File.Exists(cpath))
                {
                    DataSet ds = new DataSet();
                    ds.ReadXml(cpath);
                    tbConsl = ds.Tables[0];
                    //Load Consultant!
                    if (tbConsl != null)
                    {
                        textBox1.Text = EncodeDecode.GetDecoded(tbConsl.Rows[20][1].ToString());
                        if (textBox1.Text.Length < 2)
                        {
                            this.panel1.Visible = false;
                            this.button1.Text = "&OK";
                            this.button2.Visible = false;
                        }
                    }
                }
                else
                {
                    this.panel1.Visible = false;
                    this.button1.Text = "&OK";
                    this.button2.Visible = false;
                }
            }
            catch (Exception ex) {
                fr.MessText = ex.Message;
                //MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        
        }
    }
}